<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection"
          content="telephone=no,email=no,date=no,address=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="msapplication-tap-highlight" content="no">
    <title>付款</title>
    <link rel="stylesheet" href="/statics/css/bootstrap.min.css">
    <link rel="stylesheet" href="/statics/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/css/weui.min.css"/>
    <link rel="stylesheet" type="text/css" href="/css/jquery-weui.css"/>
    <script src="/js/jquery.min.js"></script>
    <script src="/js/jquery-weui.min.js"></script>
</head>
<body>
<div style="text-align: center;">
    <div class="explain" style="margin-bottom: 40px;padding-top: 50px;">
        <!--<img width="30px" height="30px" src="uploadQRCode/paylogo.png">-->
        <div style="font-size: 16px;margin-left:-50%;">您正在向：</div>
        <div style="font-size: 16px;">
            <img width="60%" height="98%" id="logoUrl" src="">
            <span id="merchantName"></span>
        </div>
        <div style="font-size: 16px;text-align:right;margin-right:42px"> 发起支付</div>
    </div>
    <div class="box apply_box" id="applyBox">
        <dl>
            <dd>
                <center>
                    <label style="font-size: 33px;color: red">¥</label><label style="font-size: 28px" id="payMoney">0.00</label>
                </center>
                <br/>
                <center>
                    <input type="text" name="payremark" id="payremark" class="form-control"  style="width: 80%;" placeholder="备注:">
                </center><br/>
            </dd>

        </dl>
    </div>

    <footer class="sub_wrap" style="text-align: center;padding-top: 30px">
        <input type="hidden" name="openid" id="openid" value="">
        <input type="hidden" name="aliopenid" id="aliopenid" value="">
        <input type="button" class="btn btn-success" id="buttonQrzf" style="width: 40%;height: 50px" onclick="submitBtn()" value="确认支付"/>
    </footer>
    <div style="text-align: center;padding-top: 100px">
        <span style="color:#999;font-style: italic;">由 道盈信息 提供服务支持</span>
        <p class="weui-footer__text">Copyright @2017 备案号：湘ICP备17012799</p>
    </div>
</div>
<script type="text/javascript">
    var merchantId;//商户
    var codeNo;//收款码编号
    var baseUrl;///服务器访问地址
    var baseUrlWechat;//域名地址
    var payId;//支付参数id
    var payMoney;//支付金额
    var totalMoney;//系统预设上限金额
    var userAgent;
    var openid;
    var resource = ''; //支付来源
    var shopName;//商户名称
    var logo;//商户logo
    $(function () {
        //获取网页参数
        (function ($) {
            $.getUrlParam = function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null)
                    return unescape(r[2]);
                return null;
            }
        })(jQuery);
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            resource = '0';
        }else if(ua.match(/AlipayClient/i) == "alipayclient"){ //
            resource = '1';
        } else {
            resource = '1';
        }
        codeNo = $.getUrlParam('codeNo');
        baseUrl = $.getUrlParam('baseUrl');//服务器访问地址
        baseUrlWechat = $.getUrlParam('baseUrlWechat');//服务器访问地址
        merchantId = $.getUrlParam('merchantId');
        userAgent = $.getUrlParam("User-Agent");
        openid = $.getUrlParam("openid");
        payMoney = $.getUrlParam("payMoney");
        payId = $.getUrlParam("payId");
        shopName = decodeURI($.getUrlParam("shopName"));
        logo = decodeURI($.getUrlParam("logo"));
        if(logo!=null&&logo!=""&&logo!="null"){
            $('#logoUrl').attr('src',baseUrl+"/lookImage?filePath="+logo+"&imgSize=400x100");
        }else{
            $('#merchantName').text(shopName);
        }
        $('#payMoney').text(payMoney);
    });

    //支付提交
    function submitBtn() {
        $("#buttonQrzf").attr({ disabled: "disabled" });
        var data = {
            resource: resource,
            busId: merchantId,
            codeNo: codeNo,
            payId: payId,
            payType:'2',//区分第三方还是 还是二维码发起支付
            category: '0',
            status: "0",
            amount: payMoney,
            payremark: $("#payremark").val(),
            openid: openid
        };
        $.ajax({
            type: "POST",
            url: baseUrl+"/payorder/save",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(data),
            dataType: 'json',
            success: function (result) {
                if (result.code == 0) {
                    if (userAgent == 'MicroMessenger') {
                        callpay(result.payInfo);
                    } else if (userAgent == 'AlipayClient') {
                        aliPay(result);
                    } else {
                        $.alert(""+result.msg, "", function() {
                            $("#buttonQrzf").removeAttr("disabled");
                        });
                    }
                } else {
                    $.alert(""+result.msg, "", function() {
                        $("#buttonQrzf").removeAttr("disabled");
                    });
                }
            }
        });
    }

    function jsApiCall(params) {
        WeixinJSBridge.invoke('getBrandWCPayRequest', params, function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg 将在用户支付成功后返回ok，但并不保证它绝对可靠。
                window.location.href = baseUrlWechat+"pay_success.html"
            } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                window.location.href = baseUrlWechat+"pay_failed.html"
            }
            WeixinJSBridge.call('closeWindow');
        });
    }

    function callpay(params) {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        } else {
            jsApiCall(params);
        }
    }
    function aliPay(params) {
        AlipayJSBridge.call("tradePay", {
            tradeNO: params.tradeNO
        }, function (result) {
            if (result.resultCode == '9000') {
                window.location.href = baseUrlWechat+"pay_success.html"
            } else {
                window.location.href = baseUrlWechat+"pay_failed.html"
            }
            AlipayJSBridge.call('closeWebview');  //支付宝
        });
    }

    var oldVal = "";
    function validateAmount(obj) {
        var objVal = $(obj).val();
        var isNum = /^\d{1,6}([.]\d{0,2})?$/;
        if (objVal != "" && !isNum.test(objVal)) {
            $(obj).val(oldVal);
        } else {
            oldVal = objVal;
        }
    }

/////废弃的代码 暂时留存  待将来使用
    function wxJsPay(params) {
        window.location.href = "https://pay.swiftpass.cn/pay/jspay?token_id=" + params;
    }

</script>
</body>
</html>